package middleware

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/config"
	"gitee.com/liuxuezhan/ar-platform/base-common/middleware"
	"gitee.com/liuxuezhan/ar-platform/base-common/rpc"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/platform-business-rpc/client/channel"
	"github.com/zeromicro/go-zero/core/logc"
)

func GetPlatformBusinessFunc(c config.Config) middleware.GetPlatformBusinessFunc {
	return func(ctx context.Context, dns string) string {
		rpcC, _ := rpc.NewClient(rpc.Config{
			Host:     c.PlatformBusinessRPC,
			AppName:  c.Name,
			Business: c.Name,
		})
		res, err := channel.NewChannel(rpcC).GetBusinessCodeByDomain(ctx, &channel.GetBusinessCodeByChannelRequest{
			Domain: dns,
		})
		if err != nil {
			logc.Error(ctx, err)
			return ""
		}
		return res.GetBusinessCode()
	}
}
